package tst

import java.nio.ByteBuffer
import java.nio.channels.*

//https://blog.csdn.net/u013256816/article/details/104832237

FileInputStream fileInputStream = new FileInputStream("c:/temp/testfile.txt");
FileChannel channel = fileInputStream.getChannel();
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)

println "valid:${lock.isValid()},\t shared:${lock.isShared()}"


ByteBuffer readBuffer=ByteBuffer.allocate(3);
ByteArrayOutputStream out = new ByteArrayOutputStream()
while(channel.read(readBuffer)>0)
{
	out.write(readBuffer.array(),0,readBuffer.position());
	readBuffer.clear()
};
String txt = new String(out.toByteArray(),'utf-8')
println txt;

println 'read completed'

out.close();

lock.close();
channel.close();
fileInputStream.close()